<?php
class front_groupService extends commonService{
    private $table=null;
	private $foundergroupid=null;
	
    public function __construct(){
        parent::__construct();
        $this->table=table('front_group');
		$this->table_admin=table('front_admin');
    }
	//下拉菜单
    public function with_option(){
        $query="select `id`,`name` from `{$this->table}` where `status`='1' order by `id` asc";
        $info=array();
        $info=$this->m->mselect($query);
        return $info;
    }
	//选项聚合
	//$status_data=array('-1'=>'--请选择--','0'=>'隐藏','1'=>'显示');
	public function make_option_array($data,$with_first=true,$id='id',$name='name'){
		$data=(array) $data;
		$return=array();
		if($with_first) $return[0]='--请选择--';
		foreach($data as $k=>$v){
			$return[$v[$id]]=$v[$name];
		}
		return $return;
	}
	//列出所有的控制器
    /*
	public function list_all_controllers(){
		$except=array('.','..');
		
		$project=load_pjt(1);
		
		$file=$project['CONTROLLER_DIR'].'these.names.php';
		if(file_exists($file)){
			$file_data=include($file);
			$except=array_merge($except,$file_data['except']);
		}
		$except2=array('__construct','__destruct','__call','__callStatic','__get','__set','__isset', '__unset','__sleep','__wakeup','__toString','__invoke','__set_state','__clone','s');
		$return=array();
		$files=scandir($project['CONTROLLER_DIR']);
		foreach((array) $files as $k=>$v){
			if(is_dir($project['CONTROLLER_DIR'].$v)) continue;
			if(in_array($v,$except)) continue;
			include_once($project['CONTROLLER_DIR'].$v);
			
			$tmp_arr=array();
			$tmp=get_class_methods(str_replace('.class.php','',$v));
			foreach($tmp as $m=>$w){
				if(in_array($w,$except2)) continue;
				$tmp_arr[]=$w;
			}
			$return[]=array(str_replace('Controller.class.php','',$v),$tmp_arr);
		}
		return $return;
	}
    */
    public function list_all_controllers(){
		$except=array('.','..');
		
		$project=load_pjt(1);
		
		$file=$project['CONTROLLER_DIR'].'these.names.php';
		if(file_exists($file)){
			$file_data=include($file);
			$except=array_merge($except,$file_data['except']);
		}
		$return=array();
		$files=scandir($project['CONTROLLER_DIR']);
		foreach((array) $files as $k=>$v){
			if(is_dir($project['CONTROLLER_DIR'].$v)) continue;
			if(in_array($v,$except)) continue;
			include_once($project['CONTROLLER_DIR'].$v);
			
			$tmp_arr=array();
			$tmp=str_replace('.class.php','',$v);
            $tmp_arr=call_user_func_array(array($tmp,'_right'),array());
			$return[]=array(str_replace('Controller.class.php','',$v),$tmp_arr);
		}
		return $return;
	}
    public function add_($post){
        $result=array();
		$post['priv']=serialize($post['priv_checkbox']);
		
        $vali=validate::check('front_group',$post,'add');
        if($vali){
            $result[0]=$this->model->_i($this->table,$post);
        }else{
            $result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
        }
        return $result;
    }
	
    //
    public function mod($id){
        $id=intval($id);
        if($id>0){
            return $this->model->_f($this->table,'*',$id);
        }
        return false;
    }
    public function mod_($post,$id){
        $result=array();
		$post['priv']=serialize($post['priv_checkbox']);
		
        $vali=validate::check('front_group',$post,'mod',array('id::!='=>$id));
        if($vali){
    		$result[0]=$this->model->_u($this->table,$post,$id);
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
    }
	
    public function del($id){
        $id=intval($id);
        if($id>0){
			//有子条目的不删除
			$query="select `id` from `{$this->table_admin}` where `group_id`=:id limit 1";
			if($this->m->get_field($query,array('id'=>$id))){
				return false;
			}
            return $this->model->_d($this->table,$id);
        }
        return false;
    }
	
    public function li(){
        $return=array();
        $query="select * from `{$this->table}` order by `id` asc";

        $p=new smart_pager(array($query,array()),$this->m);
        //$p->address('&cat_id=%cat_id%','cat_id',100);
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query);
            $return[]=$p;
        }
        return $return;
    }
}